package com.yanqu.road.server.cmd.activity.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.S_CROSS_ACTIVITY_SEVER_RANK_LIST, desc = "获取跨服排行榜数据")
public class CrossActivityServerRankListCmd extends AbstractCommand {

	private int activityId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossActivityProto.CrossActivityServerRankListReqMsg reqMsg = CrossActivityProto.CrossActivityServerRankListReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		CrossActivityProto.CrossActivityServerRankListRespMsg.Builder respMsg = CrossActivityProto.CrossActivityServerRankListRespMsg.newBuilder();
		CrossActivityProto.CrossActivityServerRankListMsg.Builder rankListMsg = CrossDataMgr.getActivityServerRankMsg(activityId);
		ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
		if(null != activityInfo){
			//补一下没有初始化起来的区服列表数据
			if(activityInfo.getServerIdList().size() != rankListMsg.getRankListCount()) {
				for (long serverId : activityInfo.getServerIdList()) {
					boolean isFind = false;
					for (int i = 0; i < rankListMsg.getRankListCount(); i++) {
						CrossActivityProto.CrossActivityServerRankTempMsg rankDataMsg = rankListMsg.getRankListList().get(i);
						if (rankDataMsg.getServerId() == serverId) {
							isFind = true;
						}
					}
					if(!isFind){
						CrossActivityProto.CrossActivityServerRankTempMsg.Builder serverMsg = CrossActivityProto.CrossActivityServerRankTempMsg.newBuilder();
						serverMsg.setServerId(serverId);
						serverMsg.setValue("0");
						rankListMsg.addRankList(serverMsg);
					}
				}
			}
		}
		respMsg.setRankList(rankListMsg);
		respMsg.setActivityId(activityId);
		respMsg.setMyRank(getMyRank(rankListMsg));
		player.sendPacket(Protocol.U_CROSS_ACTIVITY_SEVER_RANK_LIST, respMsg);
	}

	/**
	 * 获取我的区服排名
	 * @param rankListMsg
	 * @return
	 */
	private int getMyRank(CrossActivityProto.CrossActivityServerRankListMsg.Builder rankListMsg) {
		List<CrossActivityProto.CrossActivityServerRankTempMsg> rankList = rankListMsg.getRankListList();
		for (int i = 0; i < rankList.size(); i++) {
			CrossActivityProto.CrossActivityServerRankTempMsg rankDataMsg = rankList.get(i);
			if (rankDataMsg.getServerId() == GameServer.getInstance().getServerId()) {
				return i + 1;
			}
		}
		return -1;
	}

	@Override
	public void sendData(GamePlayer player) {

	}
}
